﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using DataModel;
using BasicDataManagement;
using System.Xml;
using System.Xml.Linq;

namespace SystemWindow
{

    /// <summary>
    /// DutyChangeWindow.xaml 的交互逻辑
    /// </summary>
    public partial class DutyChangeWindow : Window
    {
        SystemUser user;
        IOpertation op;
        string cashierip;
        private DutyChangeWindow()
        {
            InitializeComponent();
            op = OperationFactory.Create();
            cashierip = XElement.Load("Configuration.xml").Element("ThisIP").Value;
        }

        public DutyChangeWindow(SystemUser user)
            : this()
        {
            this.user = user;
            decimal totalprice = op.LoadStaffLogPrice(user.LogID);
            int dealcount = op.LoadStaffLogDealCount(user.LogID);
            decimal discount = op.LoadDiscount(user.LogID);

            currCashierTB.Text = user.StaffName + "（" + user.StaffJobNum + "）";
            loginTimeTB.Text = op.LoadLoginTime(user).ToString();
            dealTotalPriceTB.Text = totalprice.ToString() ;
            dealCountTB.Text = dealcount.ToString();
            avgPriceTB.Text = ((dealcount == 0) ? 0 : totalprice / dealcount).ToString("F2");
            discountTB.Text = discount.ToString("F2");
            cashTB.Text = (totalprice - discount).ToString("F2");
            takenMoneyTB.Text = op.LoadTakenMoney(user.LogID).ToString("F2");
            givenMoneyTB.Text = op.LoadGivenMoney(user.LogID).ToString("F2");
        }

        private void btnOK_Click(object sender, RoutedEventArgs e)
        {
            AuthorityGroup tmpGroup = op.LoadStaffGroup(confirmCashierTB.Text, confirmChashierPwdTB.Password);
            if (tmpGroup == null)
            {
                MessageBox.Show("确认人员账号密码有误！");
                return;
            }
            if (tmpGroup.ReliefPermission == false)
            {
                MessageBox.Show("无权限进行交班、确认操作");
                return;
            }
            if (changeDutyRB.IsChecked != true && Convert.ToDecimal(restTB.Text) > 0.009M)
            {
                MessageBox.Show("钱箱余款不为0，不可进行下班操作！");
                return;
            }
            if (changeDutyRB.IsChecked == true)
            {
                op.Logout(user, cashierip);
                SystemUser tmpuser = op.Login(confirmCashierTB.Text, confirmChashierPwdTB.Password,cashierip);
                (this.Owner as MainWindow).logset(tmpuser);
                this.DialogResult = true;
                this.Close();
                return;
            }
            if (changeDutyRB.IsChecked != true)
            {
                op.Logout(user,cashierip);
                (this.Owner as MainWindow).canClose = true;
                this.Owner.Close();
            }
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            XElement xmlfile = XElement.Load("MoneyBox.xml");
            restTB.Text = xmlfile.Element("Rest").Value;
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }
    }
}
